#ifndef INDEX_H
#define INDEX_H

#include <QtCore\QStringList>
#include <QtCore\QString>
#include <QtCore\QHash>
#include <QtCore\QVector>

class ObjectStorage;
class MapObject;

struct Document {
    Document( MapObject * mapObject, int f ) : mapObject ( mapObject ), frequency( f ) {}
    Document() : mapObject ( 0 ), frequency( 0 ) {}
    bool operator==( const Document &doc ) const {
	return mapObject == doc.mapObject;
    }
    bool operator<( const Document &doc ) const {
	return frequency > doc.frequency;
    }
    bool operator<=( const Document &doc ) const {
	return frequency >= doc.frequency;
    }
    bool operator>( const Document &doc ) const {
	return frequency < doc.frequency;
    }
    MapObject * mapObject;
    qint16 frequency;
};

class SEARCH_DLL_EXPORT Index
{
public:
    struct Entry {
	    Entry( MapObject * mapObject ) { documents.append( Document( mapObject, 1 ) ); }
	    Entry( QVector<Document> l ) : documents( l ) {}
	    QVector<Document> documents;
    };

    Index();
    virtual ~Index();

    void makeDict ( ObjectStorage * objStorage );
    void getObjectListByString ( string & str, vector<MapObject *> & );

	void saveDict ( QString fileName );
	void loadDict ( QString fileName, ObjectStorage * storage );

private:
	void getObjectListBySingleString ( string & str, vector<MapObject *> & );

public:
    void addMapObject ( MapObject * mapObject );
    void addString    ( MapObject * mapObject, string & str );
    void addWord      ( MapObject * mapObject, string & str );

    QHash<QString, Entry*> dict;
};

#endif // INDEX_H
